# -*- coding: utf-8 -*-
# @Time : 2021/10/22 15:01
# @Author : 张宇鹏
# @FileName: 1.py
import csv
import re
import os
# 不支持分号
# 要修饰
# 肽段所在的列
listP = 2
# 修饰所在的列
listX = 3
# 定量值所在的列
listN = 5




name_set = set()
name2value = {}
head = ["name"]
for name in os.listdir("./input"):
    print(name)
    head.append(name)
    with open("./input/" + name, encoding="utf-8") as f1:
        reader = csv.reader(f1)
        for row in reader:
            # split_list = re.split("; ", )
            name_set.add(row[listP] + ' # ' + row[listX])


nameTemp = set()
count = 0
for name in os.listdir("./input"):
    with open("./input/" + name) as f1:
        reader = csv.reader(f1)
        for row in reader:
            if row[listN] == "":
                row[listN] = 0

            #if row[listP] not in nameTemp:
            name2value.setdefault(row[listP] + ' # ' + row[listX], []).append(row[listN])

            nameTemp.add(row[listP] + ' # ' + row[listX])
    for x in name_set:
        if x not in nameTemp:
            name2value.setdefault(x, []).append(0)
    nameTemp = set()
    count = count + 1

with open("resultpep.csv", mode="w", encoding="utf-8",newline="") as f2:
    writer = csv.writer(f2)

    writer.writerow(head)
    save = []
    for x in name2value:
        save.append(x)
        for y in name2value[x]:
            save.append(y)
        writer.writerow(save)
        save = []


